package com.itxiuyixiu.homework.entity.dao;

import com.itxiuyixiu.homework.entity.model.Course;
import com.itxiuyixiu.homework.entity.vo.CourseVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 黄磊
 * @date 2020/3/5
 */
public interface CourseDao extends JpaRepository<Course, Integer> {
    boolean existsByTeacherId(int teacherId);

    boolean existsByMajorId(int majorId);

    @Transactional(readOnly = true)
    @Query(value = "select id,introduction, courseName,  gmtCreated, gmtModified, opened, fk_major_id as majorId, fk_teacher_id as teacherId from Course where id=?1" , nativeQuery = true)
    List<CourseVo> findAllById(int id);

    @Transactional(readOnly = true)
    @Query(value = "select id,introduction, courseName,  gmtCreated, gmtModified, opened, fk_major_id as majorId, fk_teacher_id as teacherId from Course where courseName=?1" , nativeQuery = true)
    List<CourseVo> findAllByCourseName(String courseName);

    @Transactional(readOnly = true)
    @Query(value = "select id,introduction, courseName, gmtCreated, gmtModified, opened, fk_major_id as majorId, fk_teacher_id as teacherId from Course where fk_major_id=?1" , nativeQuery = true)
    List<CourseVo> findAllByMajorId(int majorId);

    @Transactional(readOnly = true)
    @Query(value = "select id,introduction, courseName,  gmtCreated, gmtModified, opened, fk_major_id as majorId, fk_teacher_id as teacherId from Course where fk_teacher_id=?1" , nativeQuery = true)
    List<CourseVo> findAllByTeacherId(int teacherId);

    @Transactional(readOnly = true)
    @Query(value = "select id,introduction ,courseName,  gmtCreated, gmtModified, opened, fk_major_id as majorId, fk_teacher_id as teacherId from Course" , nativeQuery = true)
    List<CourseVo> findAllCourse();
}
